home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 January: Mac OS SDK / Dev.CD Jan 97 SDK2.toast / Development Kits (Disc 2) / OpenDoc / OpenDoc Development / Debugging Support / OpenDoc™ Source Code / UI / CmdTable.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1996-08-28  |  6.6 KB  |  268 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        CmdTable.cpp
  3.  
  4.     Contains:    CommandTable and CommandTableEntry C++ classes.
  5.  
  6.     Owned by:    Richard Rodseth
  7.  
  8.     Copyright:    © 1993 - 1995 by Apple Computer, Inc., all rights reserved.
  9.  
  10.     Change History (most recent first):
  11.  
  12.          <4>     7/25/95    VL        1270320: Delete entry after removing it in
  13.                                     UnregisterCommand.
  14.          <3>     5/26/95    RR        #1251403: Multithreading naming support
  15.          <2>     5/25/95    jpa        List.h --> LinkList.h [1253324]
  16.          <1>     5/10/94    RR        first checked in
  17.          <2>     3/15/94    MB        Changes to support SCpp/ASLM builds,
  18.                                     #1150864.
  19.          <1>     9/15/93    RR        first checked in
  20.  
  21.     To Do:
  22.     In Progress:
  23. */
  24.  
  25. #ifndef _CMDTABLE_
  26. #include "CmdTable.h"
  27. #endif
  28.  
  29.  
  30. #pragma segment ODCmdTable
  31.  
  32.  
  33. //======================================================================================
  34. // Class CommandTableEntry - Definition & Implementation
  35. //======================================================================================
  36.  
  37. class CommandTableEntry : public Link {
  38.  
  39. public:
  40.  
  41.     CommandTableEntry(ODCommandID command,
  42.                   ODMenuID    menu,
  43.                   ODMenuItemID menuItem);
  44.                   
  45.     ODVMethod ~CommandTableEntry()    {}
  46.     
  47.     ODCommandID GetCommandNumber()    { return fCommandNumber;}
  48.     void        SetCommandNumber(ODCommandID command) 
  49.                     { fCommandNumber = command;}
  50.     void        GetMenuAndItem(ODMenuID&    menu,
  51.                   ODMenuItemID& menuItem)    { menu = fMenu; menuItem = fMenuItem;}
  52.     void        SetMenuAndItem(ODMenuID    menu,
  53.                                  ODMenuItemID menuItem)    
  54.                     { fMenu = menu; fMenuItem = menuItem; }
  55.                     
  56.     ODBoolean    Matches(ODCommandID command)
  57.                     { return (fCommandNumber == command); }
  58.     ODBoolean    Matches(ODMenuID    menu,
  59.                           ODMenuItemID menuItem)
  60.                     { return (fMenu == menu) && (fMenuItem == menuItem); }
  61.  
  62. private:
  63.     ODCommandID     fCommandNumber;
  64.     ODMenuID            fMenu;
  65.     ODMenuItemID        fMenuItem;
  66. };
  67.  
  68.     
  69. CommandTableEntry::CommandTableEntry(ODCommandID command,
  70.                   ODMenuID    menu,
  71.                   ODMenuItemID menuItem)
  72. {
  73.     fCommandNumber = command;
  74.     fMenu = menu; 
  75.     fMenuItem = menuItem;
  76. }
  77.  
  78. //======================================================================================
  79. // Class CommandTable
  80. //======================================================================================
  81.  
  82. //------------------------------------------------------------------------------
  83. // CommandTable::CommandTable
  84. //
  85. // Description
  86. //------------------------------------------------------------------------------
  87.  
  88. CommandTable::CommandTable()
  89. {
  90. }
  91.  
  92. //------------------------------------------------------------------------------
  93. // CommandTable::~CommandTable
  94. //
  95. // Description
  96. //------------------------------------------------------------------------------
  97.  
  98. CommandTable::~CommandTable()
  99. {
  100.     this->UnregisterAll();
  101. }
  102.  
  103. //------------------------------------------------------------------------------
  104. // CommandTable::Copy
  105. //
  106. // Description
  107. //------------------------------------------------------------------------------
  108.  
  109. CommandTable* CommandTable::Copy()
  110. {
  111.     CommandTable* copy = new CommandTable();
  112.     
  113.     LinkedListIterator iter(&fImplementation);
  114.     CommandTableEntry* entry = (CommandTableEntry*) iter.First();
  115.     while (entry != kODNULL)
  116.     {
  117.         ODCommandID command = entry->GetCommandNumber();
  118.         ODMenuID menu;
  119.         ODMenuItemID menuItem;
  120.         entry->GetMenuAndItem(menu, menuItem);
  121.         copy->RegisterCommand(command, menu, menuItem);
  122.         entry = (CommandTableEntry*) iter.Next();
  123.     }    
  124.     return copy;
  125. }
  126.  
  127. //------------------------------------------------------------------------------
  128. // CommandTable::RegisterCommand
  129. //
  130. // Description
  131. //------------------------------------------------------------------------------
  132.  
  133. void CommandTable::RegisterCommand(ODCommandID command,
  134.                                     ODMenuID menu,
  135.                                     ODMenuItemID menuItem)
  136. {
  137.     if (this->IsCommandRegistered(command))
  138.         ;// Throw exception
  139.     else
  140.     {
  141.         CommandTableEntry* entry = new CommandTableEntry(command, menu, menuItem);
  142.         if (entry)
  143.             fImplementation.AddLast(entry);
  144.         else
  145.             ;
  146.     }
  147. }
  148.  
  149. //------------------------------------------------------------------------------
  150. // CommandTable::IsCommandRegistered
  151. //
  152. // Description
  153. //------------------------------------------------------------------------------
  154.  
  155. ODBoolean CommandTable::IsCommandRegistered(ODCommandID command)
  156. {
  157.     LinkedListIterator iter(&fImplementation);
  158.     CommandTableEntry* entry = (CommandTableEntry*) iter.First();
  159.     while (entry != kODNULL)
  160.     {
  161.         if (entry->Matches(command))
  162.         {
  163.             return kODTrue;    
  164.         }
  165.         else
  166.             entry = (CommandTableEntry*) iter.Next();
  167.     }    
  168.     return kODFalse;
  169. }
  170.  
  171. //------------------------------------------------------------------------------
  172. // CommandTable::GetCommand
  173. //
  174. // Description
  175. //------------------------------------------------------------------------------
  176.  
  177. ODCommandID CommandTable::GetCommand(ODMenuID menu,
  178.                                     ODMenuItemID menuItem)
  179. {
  180.     ODCommandID result = kNoCommand;
  181.                 
  182.     LinkedListIterator iter(&fImplementation);
  183.     CommandTableEntry* entry = (CommandTableEntry*) iter.First();
  184.     while (entry != kODNULL)
  185.     {
  186.         if (entry->Matches(menu,menuItem))
  187.         {
  188.             return entry->GetCommandNumber();
  189.         }
  190.         else
  191.             entry = (CommandTableEntry*) iter.Next();
  192.     }    
  193.     return kNoCommand;
  194. }
  195.  
  196. //------------------------------------------------------------------------------
  197. // CommandTable::GetMenuAndItem
  198. //
  199. // Description
  200. //------------------------------------------------------------------------------
  201.  
  202. ODBoolean CommandTable::GetMenuAndItem(ODCommandID command,
  203.                                     ODMenuID& menu,
  204.                                     ODMenuItemID& menuItem)
  205. {
  206.     ODBoolean found = kODFalse;
  207.     
  208.     LinkedListIterator iter(&fImplementation);
  209.     CommandTableEntry* entry = (CommandTableEntry*) iter.First();
  210.     while (entry != kODNULL)
  211.     {
  212.         if (entry->Matches(command))
  213.         {
  214.             entry->GetMenuAndItem(menu, menuItem);
  215.             found = kODTrue;
  216.             break;    
  217.         }
  218.         else
  219.             entry = (CommandTableEntry*) iter.Next();
  220.     }    
  221.     if (!found)
  222.     {
  223.         menu = 0;
  224.         menuItem = -1;
  225.     };
  226.     return found;
  227. }
  228.  
  229. //------------------------------------------------------------------------------
  230. // CommandTable::UnregisterCommand
  231. //
  232. // Description
  233. //------------------------------------------------------------------------------
  234.  
  235. void CommandTable::UnregisterCommand(ODCommandID command)
  236. {
  237.     LinkedListIterator iter(&fImplementation);
  238.     CommandTableEntry* entry = (CommandTableEntry*) iter.First();
  239.     while (entry != kODNULL)
  240.     {
  241.         if (entry->Matches(command))
  242.         {
  243.             fImplementation.Remove(*entry);
  244.             ODDeleteObject(entry);    
  245.             break;
  246.         }
  247.         else
  248.             entry = (CommandTableEntry*) iter.Next();
  249.     }    
  250. }
  251.  
  252. //------------------------------------------------------------------------------
  253. // CommandTable::UnregisterAll
  254. //
  255. // Description
  256. //------------------------------------------------------------------------------
  257.  
  258. void CommandTable::UnregisterAll()
  259. {
  260.     Link* link = fImplementation.RemoveFirst();
  261.     while (link != kODNULL)
  262.     {
  263.         delete link;
  264.         link = fImplementation.RemoveFirst();
  265.     }
  266. }
  267.  
  268.